﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public class InterceptData
    {
        /// <summary>
        /// 按给定周期截取自然时间线上最新的数据，截取长度为period+shift
        /// 如：CutList([1,2,3,4,5,6,7],4,0) => [4,5,6,7]
        /// 如：CutList([1,2,3,4,5,6,7],4,2) => [2,3,4,5,6,7]
        /// </summary>
        /// <param name="data"></param>
        /// <param name="period"></param>
        /// <param name="shift"></param>
        /// <returns></returns>
        //public static List<decimal> CutList(List<decimal> data, int period, int shift = 0)
        //{
        //    //由于传入的List位引用类型，修改原之后外层参数也被修改，所以进行拷贝副本
        //    #region 深拷贝
        //    List<decimal> declist = new List<decimal>();
        //    foreach (var item in data)
        //    {
        //        declist.Add(item);
        //    }
        //    #endregion
        //    //倒序转换
        //    declist.Reverse();
        //    //计算偏移量
        //    period += shift;
        //    //按周期截取数据
        //    List<decimal> newlist = new List<decimal>();
        //    if (declist.Count > period)
        //    {
        //        for (int i = 0; i < period; i++)
        //        {
        //            newlist.Add(declist[i]);
        //        }
        //    }
        //    else
        //    {
        //        for (int i = 0; i < declist.Count; i++)
        //        {
        //            newlist.Add(declist[i]);
        //        }
        //    }
        //    //正序恢复
        //    newlist.Reverse();
        //    return newlist;
        //}



        /// <summary>
        /// 按给定周期截取自然时间线上最新的数据，截取长度为period+shift
        /// 如：CutList([1,2,3,4,5,6,7],4,0) => [4,5,6,7]
        /// 如：CutList([1,2,3,4,5,6,7],4,2) => [2,3,4,5,6,7]
        /// </summary>
        /// <param name="data"></param>
        /// <param name="period"></param>
        /// <param name="shift"></param>
        /// <returns></returns>
        public static List<T> CutList<T>(List<T> data, int period, int shift = 0)
        {
            //由于传入的List位引用类型，修改原之后外层参数也被修改，所以进行拷贝副本
            #region 深拷贝
            List<T> declist = new List<T>();
            foreach (var item in data)
            {
                declist.Add(item);
            }
            #endregion
            //倒序转换
            declist.Reverse();
            //计算偏移量
            period += shift;
            //按周期截取数据
            List<T> newlist = new List<T>();
            if (declist.Count > period)
            {
                for (int i = 0; i < period; i++)
                {
                    newlist.Add(declist[i]);
                }
            }
            else
            {
                for (int i = 0; i < declist.Count; i++)
                {
                    newlist.Add(declist[i]);
                }
            }
            //正序恢复
            newlist.Reverse();
            return newlist;
        }

    }
}
